<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;

class IndexController extends BaseController {

	/**
	 * 后台主页显示
	 */
    public function index(){
		$this->display();
    }

    /**
     * 顶部
     */
    public function top(){
    	$this->gettypedata();
    	$this->display();
    }

    /**
     * 添加文档
     */
    public function articleadd(){
    	//session('adminuser','CHD'); // 默认设定SESSION值
    	$this->gettypedata();
    	$this->display();
    }

	public function countdown(){
		$rootid = I('get.rootid');
		if($rootid==78){

			$this->display();
		}
	}
    
    public function menuoption(){
    	echo menuoption();
    }
    public function showmenutype(){
    	echo showmenutype();
    }

    /**
     * 编辑文档
     */
    public function articleedit($id=null){
        	$id = htmlspecialchars($id);
    		if(!empty($id)){
    			$article = D('article');
    			$result = $article->where(array('id'=>$id))->find();
    		
    			$this->gettypedata();
    			$this->assign('article',$result);
    			$this->display();
    		}else{
    			$this->error('非法参数！','',1);
    		}
    }

    /**
     * 获取菜单并赋值模板
     */
    public function gettypedata(){
    	$key = I('post.key');
    	
    	$type = D(C('TABLE_TYPE'));
		$s_userinfo = session('userinfo');
    	if(!$s_userinfo['type']){
    		$where['userid'] = $s_userinfo['id'];
    		if(!empty($key)){
    			$where['typename'] = array('LIKE',"%".$key."%");
    		}
    	}else{
    		if(!empty($key)){
    			$where['typename'] = array('LIKE',"%".$key."%");
    		}else{
    			$where = null;
    		}
    	}
    	$wymaselect = $type->where($where)->order('id desc')->select();
    	if(!empty($key)){
    		if(!empty($wymaselect)){
    			foreach ($wymaselect as $v){
    				echo "<option value=".$v['id'].">".$v['typename']."</option>";
    			}
    		}else{
    			echo "<option value=\"\">无数据！</option>";
    		}
    		
    	}else{
    		$this->assign('wymaselect',$wymaselect);
    	}
    	
    } 

    /**
     * 关联数据
     */
    public function cpdata(){
    	$preid = I('get.preid');
    	$typeid = I('get.typeid');
    	$menuid = I('get.menuid');
    	
    	//获取数据
    	$article = D(C('TABLE_ARTICLE'));
    	$result = $article->find($preid);
		foreach ($result as $key=>$v){
			if($key!='id'){
				$article->$key = $v;
			}
		}
		$article->typeid = $typeid;
		$article->menuid = $menuid;
		
    	//复制出来并插入数据
    	$newresult = $article->field('id',true)->add();
    	if($newresult){
    		echo 1;
    	}else{
    		echo 0;
    	}
    }
    
    /**
     * 关联整个栏目的数据
     */
    public function cpcolumndata(){
    	$preid = I('get.preid');  //被关联的栏目ID
    	$menuid = I('get.menuid'); //要关联的栏目ID
    	$submenures = D(C('TABLE_SUBMENU'));
    	$res = $submenures->where(array('mparent'=>$preid))->select();
    	if(count($res)>0){
    		$r = $submenures->find($preid);
    		$preid = $r['mtypeid'];
    	}
    	echo $this->setbinidvalue($preid,$menuid);
    }
    protected function setbinidvalue($binid,$menuid){
    	$submenures = D(C('TABLE_SUBMENU'));
    	$data['binid'] = $binid;
    	$newresult = $submenures->where(array('id'=>$menuid))->save($data);
    	if($newresult){
    		return 1;//关联成功
    	}else if($newresult==0){
    		return 2; //已经关联过
    	}else{
    		return 0; //关联失败
    	}
    }
    
    /**
     * 二维码
     */
    public function getmaode(){
    	$domainid = I('get.domainid');
    	$rootid = I('get.rootid');
    	$data = C('WEBDOMAIN').'/rootid/'.$rootid.'.html';
    	if(!$this->qrcode($data)){
    		die('缺少数据');
    	}else{
    		echo "<img src='/".$this->qrcode($data)."' width='180' />";
    	}
    }

    public function main(){
    	$this->get_all_ma_list(1);
    }

	public function noaudit(){
		$this->get_all_ma_list(0);
	}

	/**
	 * 获取华人码列表
	 */
	protected function get_all_ma_list($state = 1){

		$type = D(C('TABLE_TYPE'));
		$sesion = session('userinfo');
		$sesion['type'] ? $where['state'] = $state : $where['userid'] = $sesion['id'];
		$count = $type->where($where)->count();
		//每页显示条数
		$num = 12;
		$page = new \Think\Page($count,$num);
		$obj = pagestyle($page);
		$show = $obj->show();
		$result = $type->order(array('id'=>'DESC'))->where($where)->limit($obj->firstRow.','.$obj->listRows)->select();

		$this->assign('list',$result);
		$this->assign('page',$show);

		//获取用户列表
		$u = A("User");
		$result = $u->userinfo();
		$this->assign('userlist',$result);
		$this->assign('getURL',getURL());
		$this->display('Index/main');
	}

	/**
	 * 执行转交业务逻辑
	 */
	public function jur(){
		$ma_id = I('post.ma_id');
		$user_id = I('post.user_id');
		$user_name = I('post.user_name');
		if(!empty($ma_id) && !empty($user_id) && !empty($user_name)){
			$type = D(C('TABLE_TYPE'));
			$type->startTrans();
			$data['userid'] = $user_id;
			$res = $type->where("id in ($ma_id)")->save($data);
			if($res >= 0){
				//同时转交文档权限
				$article = D(C('TABLE_ARTICLE'));
				$datas['issuer'] = $user_name;
				$result = $article->where("typeid in ($ma_id)")->save($datas);
				if($result || $result == 0){
					$type->commit();
				}else{
					$type->rollback();
				}
			}
		}
		$msg = $res ? array('error' => 0) : array('error' => 1);
		echo json_encode($msg);
	}

    /**
     * 菜单添加
     * 同时输出菜单下拉框
     */
    public function menuadd(){
		$id = I('get.rootid');
		$tpl = $this->get_template_name($id);
		if($tpl != 'index8' && $tpl != 'index9'){
			$this->assign('pmenu',menuoption());
			$this->display();
		}else{
			$this->error('此模板不支持子菜单操作','',1);
		}
    }

	/**
	 * 获取当前码台的模板名称
	 */
	protected function get_template_name($id = null){
		if(!empty($id)){
			$type = D(C('TABLE_TYPE'));
			$res = $type->field('template')->find($id);
			if($res){
				return $res['template'];
			}
		}
	}

	public function addma(){
		$this->assign('tpl', $this->tpl_select());
		//$this->assign('plate', $this->get_plate_id());
		$this->get_all_plateinfo();
		$this->display();
	}

	/**
	 * 调用Manages控制器，获取所有版块信息
	 */
	protected function get_all_plateinfo(){
		$manages = A('Manages');
		$res = $manages->get_all_plate_info();
		$this->assign('accounttype', session('userinfo')['account_type']);
		$this->assign('data', $res);
	}

    /**
     * XX码编辑
     * $id ID号
     */
    public function edit($id = null){
    	$id = htmlspecialchars($id);
    	if(!empty($id)){
    		$type = D(C('TABLE_TYPE'));
    		$result = $type->find($id);
    		$this->assign('result',$result);
			$this->assign('isyard',$this->isyard($id));
			$this->assign('tpl', $this->tpl_select());
			//$this->assign('plate', $this->get_plate_id());
			$this->get_all_plateinfo();
    		$this->display('editma');
    	}else{
    		$this->error('非法参数！','',1);
    	}
    }

	/**
	 * 获取资源模块
	 */
	protected function get_plate_id(){
		$plate = D(C('TABLE_PLATEMANAGES'));
		$res = $plate->where(array('status' => 1))->select();
		return $res;
	}

	/**
	 * 检查此码是否具有一物一码功能
	 */
	protected function isyard($id){
		$randnumber = D(C('TABLE_RANDNUMBER'));
		$count = $randnumber->where(array('ma_rootid'=>$id))->count();
		if($count>0){
			return 1;
		}else{
			return 0;
		}
	}

    /**
     * 菜单编辑
     * $id ID号
     */
    public function menuedit($id=null){
    	$id = htmlspecialchars($id);
    	if(!empty($id)){
    		$submenu = D(C('TABLE_SUBMENU'));
    		$result = $submenu->find($id);
    		$this->assign('result',$result);
    		$this->assign('pmenu',menuoption());
    		$this->display();
    	}else{
    		$this->error('非法参数！','',1);
    	}
    }

    /**
     * 菜单列表
     */
    public function menulist(){
    	$id = I('get.id',0);
    	if($id){
    		$type = D(C('TABLE_TYPE'));
    		$typename = $type->find($id);
    		$typearrayresult = $type->select();
			$str = W('Menutype/showmenuinfo',array($id)); //加载菜单信息
    		$this->assign('typearray',$typename);
    		$this->assign('typeallarray',$typearrayresult);
    		$this->assign('submenuresult',$str);
    		$this->gettypedata();
    		$this->display();
    	}else{
    		$this->error('非法参数！','',1);
    	}
    }

    /**
     * 删除菜单
     * $id 要删除的id号
     */
    public function menudelete($id=null){
    	if(isset($id)){
    		echo submenudel($id);
    	}else{
    		echo 4; //非法参数
    	}
    }

    /**
     * 输出内容列表，并判断是直接进入还是通过菜单进入
     */
    public function articlelist(){
    	$id = I('get.id',0);
    	if($id){
			$this->alist($id);
    	}else{
    		$this->alist();
    	}
    	$this->gettypedata();
    	$this->assign('empty',article_empty('article'));
    	$this->display();
    }

    /**
     * 输出内容列表
     */
    protected function alist($id=null){
    	if(isset($id)){
    		$where['menuid'] = array('in',$id);
    	}else{
    		$where = null;
    	}
		$userinfo = session('userinfo');
		$usertype = $userinfo['type'];
		if($usertype==0){
			$where['issuer'] = $userinfo['username'];
		}
    	$article = D(C('TABLE_ARTICLE'));
    	$count = $article->where($where)->count();
    	//每页显示条数
    	$num = 12;
    	$page = new \Think\Page($count,$num);
    	$obj = pagestyle($page);
    	$show = $obj->show();
    	$result = $article->where($where)->order(array('id'=>'DESC'))->limit(limitbakstr($obj))->select();
    	$this->assign('page',$show);
    	$this->assign('articlelist',$result);
    }  
    
    /**
     * 获取旧码列表
     */
    public function getjma(){
    	$key = I('post.key');
    	$m = M('ehrcompany',null,'ODDH5_DB_CONFIG1');
    	if(!empty($key)){
    		$where['title'] = array('LIKE',"%".$key."%");
    		$reult = $m->where($where)->order(array('id'=>'DESC'))->select();
    	}else{
    		$reult = $m->order(array('id'=>'DESC'))->select();
    	}
    	$str = "<script type='text/javascript' src='/Public/js/jquery-1.9.1.min.js'></script><script type='text/javascript' src='/Public/js/jiumaon.js'></script>";
    	if(!empty($reult)){
    		foreach ($reult as $v){
    			if(!empty($v['domain']) && $v['edittype']!=2 && $v['edittype']!=0){
    				//$isjma = "(旧码)";
    				$str .= "<li v='".$v['id']."'><label>".$v['id']."</label><b>".$v['title']."</b></li>";
    			}
    		}
    		echo $str;
    	}else{
    		echo 0;
    	}
    }
    
    /**
     * 同步
     */
    public function tongbu(){
    	$newid = I('post.newid');
    	$oddid = I('post.oddid');
    	$type = D(C('TABLE_TYPE'));
    	$type->domainid = $oddid;
    	$emptyodd = $type->where(array('domainid'=>$oddid))->select();
    	if(count($emptyodd)>0){
    		echo -1;
    	}else{
    		$saveresult = $type->where(array('id'=>$newid))->save();
    		if($saveresult==0){
    			echo 0;
    		}else{
    			if($saveresult){
    				echo 1;
    			}else{
    				echo 2;
    			}
    		}
    	}
    }

	/**
	 * 取消分类关联
	 */
    public function cancelgl(){
		$id = I('get.id');
		if(is_numeric($id)){
			$submenu = D(C('TABLE_SUBMENU'));
			$submenu->binid = null;
			$res = $submenu->where(array('id' => $id))->save();
			if($res){
				$this->success('取消关联成功', '', 1);
			}else{
				$this->error('取消关联失败', '', 1);
			}
		}
	}

    /**
     * 获取诚信节目信息
     */
    public function getcxinfo(){
    	$key = I('post.key');
    	$m = M('t_act_player',null,'ODDH5_DB_CONFIG2');
    	$where['f_act_id'] = 18;
    	if(!empty($key)){
    		$where['p_name'] = array('LIKE',"%".$key."%");
    		$reult = $m->where($where)->order(array('id'=>'DESC'))->field('id,p_name,p_number')->select();
    	}else{
    		$reult = $m->where($where)->order(array('id'=>'DESC'))->field('id,p_name,p_number')->select();
    	}
    	$str = "<script type='text/javascript' src='/Public/js/jquery-1.9.1.min.js'></script><script type='text/javascript' src='/Public/js/getcxaddress.js'></script>";
    	if(!empty($reult)){
    		foreach ($reult as $v){
    			$str .= "<li v1='".$v['id']."'><label style=\"width:50px;\">".$v['id']."</label><b>".$v['p_name']."</b><h4>".$v['p_number']."</h4></li>";
    		}
    		echo $str;
    	}else{
    		echo 0;
    	}
    }

	/**
	 * 获取后台分类列表
	 */
	public function left(){
		$manages = A('Manages');
		$res = $manages->getallinfo();
		$this->assign('data',$res);
		$this->display();
	}

	/**
	 * 浮画管理
	 */
	public function fuhua($rootid = null){
		$fuhua = D(C('TABLE_FUHUA'));
		$res = $fuhua->where(array('rootid' => $rootid))->find();
		if($res){
			$this->assign('fuhua', $res);
			$this->assign('f_states', 1);
		}else{
			$this->assign('f_states', 0);
		}
		$this->assign('rootid', $rootid);
		$this->display();
	}

	/**
	 * 浮画添加或修改
	 */
	public function fuhua_operation(){
		$states = I('post.states');
		$rootid = I('post.rootid');

		$fuhua = D(C('TABLE_FUHUA'));
		$fuhua->create();
		if($states == 1){
			$result = $fuhua->where(array('rootid' => $rootid))->save();
		}else{
			$fuhua->rootid = $rootid;
			$result = $fuhua->add();
		}
		if($result >= 0){
			$this->success('操作成功！',U('Index/main'));
		}else{
			$this->error('操作失败','',1);
		}
	}

    /**
     * 根据版块ID获取所属版块信息
     */
    public function get_palte_subinfo($plate_id = null){
        $platemanages = D(C("TABLE_PLATEMANAGES"));
        $res = $platemanages->find($plate_id);
        $platecontent = D(C("TABLE_PLATECONTENT"));
        $result = $platecontent->where(array('plate_id' => $plate_id, 'state' => 1))->limit(2)->select();
        $res['subinfo'] = $result;
        //dump($res);
        echo json_encode($res);
    }



}